cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        if (!cc.vv) {
            return;
        }

        var gameChild = this.node.getChildByName("game");
        var myself = gameChild.getChildByName("myself");
        var pengangroot = myself.getChildByName("penggangs");
        var realwidth = cc.director.getVisibleSize().width;
        var scale = realwidth / 1280;
        pengangroot.scaleX *= scale;
        pengangroot.scaleY *= scale;
        // console.log("估摸着美欧")
        var self = this;
        this.node.on('peng_notify', function (data) {
            //刷新所有的牌
            //console.log(data.detail);
            var data = data.detail;
            self.onPengGangChanged(data);
        });

        this.node.on('chi_notify', function (data) {
            //刷新所有的牌
            var data = data.detail;
            // console.log(data);
            self.onPengGangChanged(data);
        });

        this.node.on('gang_notify', function (data) {
            //刷新所有的牌
            //console.log(data.detail);
            var data = data.detail;
            self.onPengGangChanged(data.seatData);
        });

        this.node.on('game_begin', function (data) {
            self.onGameBein();
        });

        this.node.on('game_sync', function (data) {
            for (var i in seats) {
                self.onPengGangChanged(seats[i]);
            }
        });

        this.node.on('game_over', function (data) {
            for (var i in seats) {
                self.onPengGangChanged(seats[i], true);
            }
        });

        var seats = cc.vv.gameNetMgr.seats;
        for (var i in seats) {
            if (seats[i] != null) {
                self.onPengGangChanged(seats[i]);
            }
        }


        // cc.vv.pengang = this;
    },

    onGameBein: function () {
        this.hideSide("myself");
        this.hideSide("right");
        this.hideSide("up");
        this.hideSide("left");
    },

    hideSide: function (side) {
        var gameChild = this.node.getChildByName("game");
        var myself = gameChild.getChildByName(side);
        var pengangroot = myself.getChildByName("penggangs");
        if (pengangroot) {
            for (var i = 0; i < pengangroot.childrenCount; ++i) {
                pengangroot.children[i].active = false;
            }
        }
    },

    onPengGangChanged: function (seatData, bool) {
        if (seatData.angangs == null && seatData.diangangs == null && seatData.wangangs == null && seatData.pengs == null && seatData.chis == null) {
            return;
        }
        var localIndex = cc.vv.gameNetMgr.getLocalIndex(seatData.seatindex);
        var side = cc.vv.mahjongmgr.getSide(localIndex);
        var pre = cc.vv.mahjongmgr.getFoldPre(localIndex);

        var gameChild = this.node.getChildByName("game");
        var myself = gameChild.getChildByName(side);
        var pengangroot = myself.getChildByName("penggangs");

        for (var i = 0; i < pengangroot.childrenCount; ++i) {
            pengangroot.children[i].active = false;
        }
        //初始化杠牌
        var index = 0;

        var gangs = seatData.angangs
        for (var i = 0; i < gangs.length; ++i) {
            var mjid = gangs[i];
            this.initPengAndGangs(pengangroot, side, pre, index, mjid, "angang", bool);
            index++;
        }
        var gangs = seatData.diangangs
        for (var i = 0; i < gangs.length; ++i) {
            var mjid = gangs[i];
            this.initPengAndGangs(pengangroot, side, pre, index, mjid, "diangang");
            index++;
        }

        var gangs = seatData.wangangs
        for (var i = 0; i < gangs.length; ++i) {
            var mjid = gangs[i];
            this.initPengAndGangs(pengangroot, side, pre, index, mjid, "wangang");
            index++;
        }

        //初始化碰牌
        var pengs = seatData.pengs
        if (pengs) {
            for (var i = 0; i < pengs.length; ++i) {
                var mjid = pengs[i];
                this.initPengAndGangs(pengangroot, side, pre, index, mjid, "peng");
                index++;
            }
        }

        //初始化吃牌
        var chis = seatData.chis
        if (chis) {
            for (var i = 0; i < chis.length; ++i) {
                var mjid = chis[i];
                this.initPengAndGangs(pengangroot, side, pre, index, mjid, "chi");
                index++;
            }
        }

    },

    initPengAndGangs: function (pengangroot, side, pre, index, mjid, flag, bool) {
        var pgroot = null;
        if (pengangroot.childrenCount <= index) {
            if (side == "left" || side == "right") {
                pgroot = cc.instantiate(cc.vv.mahjongmgr.pengPrefabLeft);
            }
            else {
                pgroot = cc.instantiate(cc.vv.mahjongmgr.pengPrefabSelf);
            }

            pengangroot.addChild(pgroot);
        }
        else {
            pgroot = pengangroot.children[index];
            pgroot.active = true;
        }
        var indexU = index * 10;
        if (side == "left") {
            pgroot.y = -(index * 25 * 3) - index * 10 + 20;
            pgroot.x = -540;
        }
        else if (side == "right") {
            pgroot.y = (index * 25 * 3) + index * 10 - 20;
            pgroot.setLocalZOrder(-index);
            pgroot.x = +540;
        }
        else if (side == "myself") {
            pgroot.x = index * 55 * 3 + index * 10 + 160;
            pgroot.y = -20;
        }
        else {
            pgroot.x = -(index * 55 * 3) + index * -10 + 10;
            pgroot.y = 48;
        }

        var sprites = pgroot.getComponentsInChildren(cc.Sprite);
        for (var s = 0; s < sprites.length; ++s) {
            var sprite = sprites[s];
            if (sprite.node.name == "gang") {
                var isGang = flag != "peng";
                sprite.node.active = isGang;
                sprite.node.scaleX = 1.0;
                sprite.node.scaleY = 1.0;

                if (flag == "angang") {
                    if (side == "myself" || side == "up") {
                        sprite.node.scaleX = 1.4;
                        sprite.node.scaleY = 1.4;
                    }
                    if (side == "myself" || bool) {
                        sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
                    } else {
                        sprite.spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(side);
                    }
                }
                else if (flag == "chi" || flag == "peng") {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, "");
                }
                else {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
                }
            }
            else {
                if (flag == "chi") {
                    sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid[s]);
                } else {
                    if (flag == "angang") {
                        if (bool) {
                            sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
                        } else {
                            sprite.spriteFrame = cc.vv.mahjongmgr.getEmptySpriteFrame(side);
                        }
                    } else {
                        sprite.spriteFrame = cc.vv.mahjongmgr.getSpriteFrameByMJID(pre, mjid);
                    }
                }

            }
        }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
